cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(AMCAXDatakits)

if (NOT DEFINED CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif()

include (utils.cmake)

set(LINUX_OS_VERSION OFF)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  include(${CMAKE_SOURCE_DIR}/cmake/check_glibc_version.cmake)
  set(REQUIRED_GLIBC_VERSION 300) # 表示 2.28
  get_glibc_version()
  
  if(${CURRENT_GLIBC_VERSION} LESS ${REQUIRED_GLIBC_VERSION})
    set(LINUX_OS_VERSION ON)
  endif()
endif()

if(WIN32)
  add_compile_options("/std:c++17")
  add_compile_definitions(_USE_MATH_DEFINES)
endif()

include_directories(${CMAKE_SOURCE_DIR}/src)
include_directories(${CMAKE_SOURCE_DIR}/include)



# 3rd
set(CONAN_DIR ${CMAKE_SOURCE_DIR}/.conan/${CMAKE_BUILD_TYPE})
include(${CONAN_DIR}/conan_toolchain.cmake)

# spdlog
find_package(spdlog REQUIRED)
list(APPEND CONAN_PRIVATE_LIBRARIES spdlog::spdlog)

find_package(threadpool REQUIRED)
list(APPEND CONAN_PRIVATE_LIBRARIES threadpool::threadpool)



message(STATUS "DTK_INCLUDE_DIRECTORY is ${DTK_INCLUDE_DIRECTORY}")
include_directories(${DTK_INCLUDE_DIRECTORY})


set (DTK_LIBS
	DatakitRead
	Lib3dmRead
	LibCatiaV4Read
	LibCatiaV5Read
	LibCatiaV6Read
	LibCercon2Read
	LibCerecRead
	LibCgmRead
	LibCreoViewRead
	LibDcmRead
	LibF3dRead
	LibFbxReadWrite
	LibGltfReadWrite
	LibIgesRead
	LibInventorRead
	LibJtRead
	LibLava3MRead
	LibNavisRead
	LibParasolidRead
	LibPdfWrite
	LibPlmXmlRead
	LibPrcReadWrite
	LibProCeraRead
	LibProERead
	LibQifRead
	LibRevitRead
	LibSeRead
	LibSmgRead
	LibStepRead
	LibStlRead
	LibTesselation
	LibUgSwSeRead
	LibUtil
	LibVdaRead
	TsLib
	LibParasolidWrite
	LibSwWrite
	Lib3DXmlWrite
	Lib3mfWrite
	LibCatiaV5Write
	LibCgrWrite
	LibColladaWrite
	LibIfcWrite
	LibIgesWrite
	LibJtWrite
	LibObjWrite
	LibSatWrite
	LibStepWrite
	LibUgWrite
)

if (UNIX)
	set(DTK_WIN_LIBS
	)
	if (CENTOS)
		list(REMOVE_ITEM DTK_LIBS LibDwgRead)
	endif()
elseif(NOT ${MY_PLATFORM_X64_X86} STREQUAL "arm64")	
	set(DTK_WIN_LIBS
		LibCaddsRead
	)	
endif()


if((CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Windows") AND ${MY_PLATFORM_X64_X86} STREQUAL "arm64")
	list(REMOVE_ITEM DTK_LIBS LibSeRead)
	list(REMOVE_ITEM DTK_LIBS LibDwgRead)
endif()


add_library(DTK_LIBS OBJECT IMPORTED)
add_library(DTK_WIN_LIBS OBJECT IMPORTED)


# build lib
message(STATUS "build dynamic library")
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
file(GLOB_RECURSE PROJECT_SRC src/*.cpp src/*.hpp)
add_library(${PROJECT_NAME} SHARED  ${PROJECT_SRC} $<TARGET_OBJECTS:DTK_LIBS> $<TARGET_OBJECTS:DTK_WIN_LIBS> )

target_include_directories(${PROJECT_NAME} PRIVATE ${DTK_INCLUDE_DIRECTORY})

target_link_libraries(${PROJECT_NAME} PRIVATE ${CONAN_PRIVATE_LIBRARIES} )
target_link_libraries(${PROJECT_NAME} PUBLIC ${CONAN_PUBLIC_LIBRARIES})

# if(${LINUX_OS_VERSION})
# 	target_link_libraries(${PROJECT_NAME} PRIVATE stdc++fs)
# endif()

# we add preprocessor options
if(UNIX)
 	target_compile_options(${PROJECT_NAME} PRIVATE
 		-DLinux
 		-DCOMPILATEUR=6
 		)
	if(CENTOS)
		add_definitions(-DCENTOS)
	endif()

endif()

if((CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Windows") AND ${MY_PLATFORM_X64_X86} STREQUAL "arm64")
	add_definitions(-DARM64)
endif()

if(${MY_PLATFORM_X64_X86} EQUAL "x64")
	target_compile_options(${PROJECT_NAME} PRIVATE -D_X64)
endif()


if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
      message(STATUS "GCC version: ${CMAKE_CXX_COMPILER_VERSION}")

      if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.4)
          
          add_definitions(-DCXX_LESS_17)
      else()
          target_link_libraries(${PROJECT_NAME} PRIVATE stdc++fs )
      endif()
  endif()



if(UNIX)
	if(NOT APPLE)
		target_link_libraries (${PROJECT_NAME} PRIVATE "-Wl,--start-group")
	endif()

	# We add Datakit Libraries
	target_link_libraries (${PROJECT_NAME} PRIVATE "-L ${DTK_LIB_DIRECTORY}")
	foreach (_library ${DTK_LIBS})
		message(${_library})
		message("${DTK_LIB_DIRECTORY}lib${_library}_datakit${MY_ARCH_SUFFIX}.a")
		target_link_libraries(${PROJECT_NAME} PRIVATE "${_library}_datakit${MY_ARCH_SUFFIX}.a")
	endforeach()

	
	if(NOT APPLE)
		target_link_libraries (${PROJECT_NAME} PRIVATE "-Wl,--end-group")
	endif()
	# if we have GCC we add the link to pthread library
	target_link_libraries (${PROJECT_NAME} PRIVATE "-lpthread -ldl" )
	if ( APPLE )
		target_link_libraries (${PROJECT_NAME} PRIVATE "-lcurl" )
		target_link_libraries (${PROJECT_NAME} PRIVATE "-dynamic -framework CoreFoundation -framework CoreServices")
	endif()

	
else()# if(UNIX)

	# We group source and include files in VS Explorer
	source_group("source" "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DTK_MAIN_SOURCE})
	source_group("source/RunThroughAPI/GetStarted" "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DTK_GETSTARTED_SOURCES})
	source_group("source/RunThroughAPI/ThroughAssemblies" "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DTK_ASSEMBLY_SOURCES})
	source_group("source/RunThroughAPI/ThroughData" "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DTK_DATA_SOURCES})
	source_group("source/TesselationEngine" "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DTK_TESSELATION_SOURCES})
	source_group("source/WritingSample/PdfWrite" "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DTK_PDFW_SOURCES})
	source_group("source/WritingSample/XmlWrite" "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DTK_XMLW_SOURCES})

	source_group("include" "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DTK_SDK_INCLUDES})
	source_group("include/TesselationEngine" "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DTK_TESSELATION_INCLUDES})
	source_group("include/WritingSample" "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DTK_WRITING_INCLUDES})
	
	# We add Datakit Libraries
	message(STATUS "target link directories: ${DTK_LIB_DIRECTORY}")
	target_link_directories(${PROJECT_NAME} PUBLIC ${DTK_LIB_DIRECTORY})

	foreach (_library ${DTK_LIBS})
		message(${_library})
		target_link_libraries(${PROJECT_NAME} PRIVATE debug "${_library}_MDdNet${MY_COMPILER_NAME}${MY_ARCH_SUFFIX}.lib")
		target_link_libraries(${PROJECT_NAME} PRIVATE optimized "${_library}_MDNet${MY_COMPILER_NAME}${MY_ARCH_SUFFIX}.lib")
	endforeach()
	
	foreach (_library ${DTK_WIN_LIBS})
		message(${_library})
		target_link_libraries(${PROJECT_NAME} PRIVATE debug "${_library}_MDdNet${MY_COMPILER_NAME}${MY_ARCH_SUFFIX}.lib")
		target_link_libraries(${PROJECT_NAME} PRIVATE optimized "${_library}_MDNet${MY_COMPILER_NAME}${MY_ARCH_SUFFIX}.lib")
	endforeach()

	# we add additional system32 libraries
	target_link_libraries( ${PROJECT_NAME} PRIVATE netapi32.lib )
	target_link_libraries( ${PROJECT_NAME} PRIVATE comctl32.lib )
	target_link_libraries( ${PROJECT_NAME} PRIVATE psapi.lib )
	target_link_libraries( ${PROJECT_NAME} PRIVATE Wbemuuid.lib )
	target_link_libraries( ${PROJECT_NAME} PRIVATE dhcpcsvc.lib )
	target_link_libraries( ${PROJECT_NAME} PRIVATE Shlwapi.lib )
	target_link_libraries( ${PROJECT_NAME} PRIVATE ws2_32.lib )
	if( (MSVC_VERSION GREATER_EQUAL  1900) ) #if NET2015 or greater
			target_link_libraries( ${PROJECT_NAME} PRIVATE legacy_stdio_wide_specifiers.lib )
			target_link_libraries( ${PROJECT_NAME} PRIVATE legacy_stdio_definitions.lib )
		target_link_libraries( ${PROJECT_NAME} PRIVATE userenv.lib )
	endif()


	message("DTK_READERS_BIN_FOLDER : ${DTK_READERS_BIN_FOLDER}")


endif()# if(UNIX)

file(GLOB_RECURSE TEST_SRC test/*.cpp)
add_executable(TestDataKit ${TEST_SRC})

target_include_directories(TestDataKit PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(TestDataKit ${PROJECT_NAME})




# copy share lib into CMAKE_CURRENT_BINARY_DIR
include(${CMAKE_SOURCE_DIR}/cmake/copy_conan_libs.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/get_conan_lib_dirs.cmake)
copy_conan_libs(${CMAKE_CURRENT_BINARY_DIR})
copy_conan_libs(${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE})

set(CMAKE_INSTALL_PREFIX ${PROJECT_BINARY_DIR}/install/${CMAKE_BUILD_TYPE})
install(
    TARGETS ${PROJECT_NAME}
    EXPORT ${PROJECT_NAME}Targets
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
    INCLUDES DESTINATION  include
    )
install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ 
    DESTINATION include)

# install(FILES ${CMAKE_SOURCE_DIR}/3rd/3rd.tar.xz 
#     DESTINATION ${CMAKE_INSTALL_PREFIX})

# install(SCRIPT ${CMAKE_SOURCE_DIR}/cmake/extract_tar_xz.cmake)

